<?php
require_once(dirname(dirname(__FILE__)) . '/app.php');
require_once(dirname(dirname(__FILE__)) . '/wxpay/jssdk.php');


$jssdk = new JSSDK($INI['system']['appid'], $INI['system']['appscert']);
$signPackage = $jssdk->GetSignPackage();

$cur_foot = 'mygroups';
need_login();
$order_id = $id = strval(intval($_GET['id']));


if(!$order_id || !($order = Table::Fetch('order', $order_id))) {
	die('404 Not Found');
}

$is_myslf = true;//是否是自己的拼团
if ( $order['user_id'] != $login_user['id']) {
		$is_myslf = false;
	//redirect( WEB_ROOT . "/team.php?id={$order['team_id']}");
}



$pin_order_list_info = DB::LimitQuery('pin_order_list', array(
	'condition' => array('order_id'=>$order_id),
	'one' => true,
));

$pin_order = DB::LimitQuery('pin_order', array(
	'condition' => array('id'=>$pin_order_list_info['pin_id']),
	'one' => true,
));

$pin_order_list = DB::LimitQuery('pin_order_list', array(
	'condition' => array('pin_id'=>$pin_order_list_info['pin_id']),
	'order' => 'ORDER BY createtime ASC',
));


$end_time = $pin_order['createtime'] + 86400*10;

$end_date = date('Y-m-d H:i:s',$pin_order['endtime']);
if($pin_order['state'] ==0 && $pin_order['endtime'] < time()){
	$pin_order['state'] = 2;
}

if(isset($INI['system']['secsite']) && !empty($INI['system']['secsite']))
	$pin_url = $INI['system']['wwwprefix'].$INI['system']['secsite']. "team/buy.php?id={$order[team_id]}&act=pin&pin_id={$pin_order[id]}";
else 
	$pin_url = $INI['system']['wwwprefix'] . "team/buy.php?id={$order[team_id]}&act=pin&pin_id={$pin_order[id]}";



if ($order['user_id']==$login_user['id']&& $order['state']=='unpay' && $order['rstate']=='normal' && $pin_order['state'] == 0) {
	//未支付，并且未申请退款的订单,别人是否会看到这个分享出去的未支付订单?如果这个拼团已经结束了呢？该商品是否会有新的未支付订单存在？
	Session::Set('error', '订单未支付，请支付参团.');//异常处理，该订单尚未支付
	if(isset($INI['system']['secsite']) && !empty($INI['system']['secsite']))
		redirect( $INI['system']['wwwprefix'].$INI['system']['secsite'] . "team/buy.php?id={$order[team_id]}&act=pin&pin_id={$pin_order[id]}");
	else 
		redirect( $INI['system']['wwwprefix'] . "team/buy.php?id={$order[team_id]}&act=pin&pin_id={$pin_order[id]}");
}

$tuanzhang_info = array();
$i = 0;

$has_take_in = false;//是否已经参加了拼团呢?

$time_line = array();

$buy_user_list = array();


foreach($pin_order_list as $val)
{
	$tmp_order = Table::Fetch('order', $val['order_id']);	
	if(empty($tmp_order) || ( $tmp_order['state']!= 'pay'&& $tmp_order['rstate']=='normal') )
	{
		continue;
	}
	
	
	if ( $tmp_order['user_id'] == $login_user['id']) {
		$has_take_in = true;//已经参加了。
	}
	$tmp_user = Table::Fetch('user', $tmp_order['user_id']);	
	$tmp_user['pay_time'] = date('Y-m-d H:i:s',$tmp_order['pay_time']);
	$buy_user_list[] = $tmp_user;
	//user id 
	//order  user_id team_id state
	if($i == 0)
	{
		$tmp_user['open_time'] = date('Y-m-d H:i:s',$val['createtime']);
		$tuanzhang_info = $tmp_user;
	} else {
		$time_line[] = $tmp_user;
	}
	$i++;
}
$buy_count = $i;



$team = Table::FetchForce('team', $order['team_id']);
$partner = Table::Fetch('partner', $order['partner_id']);
$express = ($team['delivery']=='express');
if ( $express ) { $option_express = Utility::OptionArray(Table::Fetch('category', array('express'), 'zone'), 'id', 'name'); }

if ( $team['delivery'] == 'coupon' ) {
	$cc = array(
			'user_id' => $login_user['id'],
			'team_id' => $order['team_id'],
			'order_id' => $order['id'],
			);
	$coupons = DB::LimitQuery('coupon', array(
				'condition' => $cc,
				));
} else if ( $team['delivery'] == 'voucher' ) {
	ZVoucher::Assign($order);
	$cc = array(
			'user_id' => $login_user['id'],
			'team_id' => $order['team_id'],
			'order_id' => $order['id'],
			);
	$vouchers = DB::LimitQuery('voucher', array(
				'condition' => $cc,
				));
}
$show = isset($_GET['show']) ? true : false;
//$pin_order['state']
//$login_user['id'] avatar
$user = Table::Fetch('user', $login_user['id']);
include template('group_view');
